package com.web.controller;

import com.web.entity.Student;
import com.web.service.StudentService;
import com.web.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/updateStudent")
public class UpdateStudent extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置请求编码
        request.setCharacterEncoding("UTF-8");
        //设置响应编码
        response.setCharacterEncoding("UTF-8");
        //设置响应类型
        response.setContentType("application/json;charset=UTF-8");
        //获取请求参数
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        String gender = request.getParameter("gender");
        String idcard = request.getParameter("idcard");
        String address = request.getParameter("address");
        String id = request.getParameter("id");
        //创建业务逻辑层对象
        StudentService studentService = new StudentServiceImpl();
        //判断信息是否输入完整
        if (id != null && name != null && age != null && gender != null && idcard != null && address != null){
            //实现修改学生信息
            boolean flag = studentService.updateStudent(new Student(name, Integer.parseInt(age), gender, idcard, address, Integer.parseInt(id)));
            if (flag){
                response.getWriter().write("修改成功！");
            }else {
                response.getWriter().write("修改失败！");
            }
        }else {
            response.getWriter().write("信息不完整！");
        }
    }
}
